package com.rovinj_server.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.rovinj_server.R;
import com.rovinj_server.entity.Inventory;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.HashMap;
import java.util.List;

public class InventoryRecordAdapter extends BaseAdapter {

    private Context mContext;
    //private ArrayList<Map> mList;
    private List<Inventory> mList;
    private HashMap<Integer, Boolean> isSelected;

    public InventoryRecordAdapter(Context context, List<Inventory> mList) {
        this.mContext = context;
        this.mList = mList;
        isSelected = new HashMap<Integer, Boolean>();
        initDate();
    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < mList.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
//		return mList.size();

        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        // TODO Auto-generated method stub
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewgroup) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {

            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.select_scan_record_item, viewgroup, false);

            holder.name = (TextView) convertView.findViewById(R.id.op_name);
            holder.code = (TextView) convertView.findViewById(R.id.op_code);
            holder.color = (TextView) convertView.findViewById(R.id.op_color);
            holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        }
        holder.name.setText(mList.get(i).getNumber());
        holder.code.setText(mList.get(i).getDepotName());
       // holder.color.setText(mList.get(i).getNums()+"");
        viewgroup.setBackgroundResource(R.drawable.usermainpage_bg_center);
        holder.cb.setChecked(getIsSelected().get(i));
        return convertView;
    }

    public void setList(List<Inventory> list) {
        mList = list;
    }

    public static class ViewHolder {

        public TextView name;
        public TextView code;
        public TextView color;

        public CheckBox cb;
    }
}
